package com.xb.exception.handler;

import com.xb.exception.BizException;
import com.xb.utils.ResultUtil;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理业务异常
     *
     * @param e 异常信息
     * @return 带有异常信息的响应实体
     */
    @ExceptionHandler(BizException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultUtil<Void> handleBizException(BizException e, HttpServletRequest request) {
        System.out.println("发生业务异常请求接口地址:" + request.getRequestURI());
        return ResultUtil.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getLocalizedMessage());
    }

    /**
     * 处理其他异常
     *
     * @param e 异常信息
     * @return 带有异常信息的响应实体
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultUtil<Void> handleOtherException(Exception e, HttpServletRequest request) {
        e.printStackTrace();
        System.out.println("发生其他异常请求接口地址:" + request.getRequestURI());
        return ResultUtil.error();
    }
}
